<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/22  16:46
// +----------------------------------------------------------------------
namespace Admin\Controller;


class CardHandleController extends CashierBaseController{

    function index(){
        $this->display();
    }

    function doHandle(){
        $data=I("post.");
        $c=M("Card")->where(["card_id"=>$data['card_id']])->count();
        if($c>0){
            $this->error("此卡已开通");
        }

        $User=M("User");
        $User->startTrans();
        $user_id=$User->data(["nickname"=>$data['child_name'],"sex"=>$data['child_sex']])->add();
        if(!$user_id){
            $User->rollback();
            $this->error("开通失败");
        }
        $data['user_id']=$user_id;
        $r=M("Card")->data($data)->add();
        if($r){
            $User->commit();
            $this->success("开通成功",U("cardShow","card_id=".$r));
        }else{
            $User->rollback();
            $this->error("开通失败");
        }
    }

    function cardShow(){
        $card_id=I("get.card_id");
        $d=M("Card")->where(["card_id"=>$card_id])->find();
        if($d){
            $d['child_sex']=$d['child_sex']==1?"男":"女";
        }
        $this->assign("d",$d);
        $this->display();
    }

}